package hd3.examples;

import hdapi3.HD3;
import hdapi3.HD3Util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class TestExample extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private JsonElement normal = null;
	private JsonElement mobile = null;
	private void setupData() {
		normal = HD3Util.parseJson(getServletContext().getResourceAsStream("/WEB-INF/normal_test_data.json"));
		mobile = HD3Util.parseJson(getServletContext().getResourceAsStream("/WEB-INF/mobile_test_data.json"));
	}
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Utility.initHDAPISettings(this.getServletContext());
		setupData();
		response.setContentType("text/html");
		
		PrintWriter out = response.getWriter();
		HashMap<String,String> headers = Utility.getRequestHeaders(request);

		HD3 hd = new HD3();
		hd.setup(headers, request.getRemoteAddr(), request.getRequestURI());
		int i = 1;
		long startTS = 0;
		long endTS = 0;
		int normalCount = 0;
		int mobileCount = 0;
		int totalCount = 0;
		if (normal != null && normal.isJsonArray()) {
			JsonArray normals = (JsonArray) normal;
			Iterator<JsonElement> iter = normals.iterator();
			long start = System.currentTimeMillis();
			while (iter.hasNext()) {
				JsonElement item = iter.next();
				if (item.isJsonObject()) {
					JsonObject obj = (JsonObject) item;
					Set<Map.Entry<String, JsonElement>> entries = obj.entrySet();
					Iterator<Map.Entry<String, JsonElement>> iter2 = entries.iterator();
 					while (iter2.hasNext()) {
 						Map.Entry<String, JsonElement> item2 = iter2.next();
 						hd.addDetectVar(item2.getKey(), item2.getValue().getAsString());
 					}
 					
 					if (i <= 5 ) {
 						startTS = System.currentTimeMillis();
 					}
					if (hd.siteDetect()) {
						out.println(totalCount + " " + obj.get("user-agent").getAsString() + " " + hd.getReply() + "<br/><br/>");
						mobileCount ++;
					} else {
						normalCount ++;
					}
					if (i <= 5) {
						endTS = System.currentTimeMillis();
						out.println("Normal Detection #" + i + " duration: " + (endTS - startTS) + "<br/>");
					}
					i ++;
					totalCount ++;
				}
			}
			long end = System.currentTimeMillis();
			out.println("Total Normal Detect duration:" + (end - start));
		} else {
			out.println("Invalid test data in file /WEB-INF/normal_test_data.json");
		}
		out.println("Expecting all detections to be other : Got " + mobileCount + " Mobiles and "+ normalCount + " Others<br/>");
		normalCount = 0;
		mobileCount = 0;
		totalCount = 0;
		i = 1;
		if (mobile != null && mobile.isJsonArray()) {
			JsonArray mobiles = (JsonArray) mobile;
			Iterator<JsonElement> iter = mobiles.iterator();
			long start = System.currentTimeMillis();
			while (iter.hasNext()) {
				JsonElement item = iter.next();
				if (item.isJsonObject()) {
					JsonObject obj = (JsonObject) item;
					Set<Map.Entry<String, JsonElement>> entries = obj.entrySet();
					Iterator<Map.Entry<String, JsonElement>> iter2 = entries.iterator();
 					while (iter2.hasNext()) {
 						Map.Entry<String, JsonElement> item2 = iter2.next();
 						hd.addDetectVar(item2.getKey(), item2.getValue().getAsString());
 					}
 					long start1 = System.currentTimeMillis();
 					if ( i <= 5) {
 						startTS = System.currentTimeMillis();
 					}
					if (hd.siteDetect()) {
						mobileCount ++;
					} else {
						out.println(totalCount + " " + obj.get("user-agent").getAsString() + " " + hd.getReply() + "<br/><br/>");
						normalCount ++;
					}
					if ( i <= 5) {
 						endTS = System.currentTimeMillis();
 						out.println("Mobile Detection #" + i + " duration: " + (endTS - startTS) + "<br/>");
 						
 					}
					i++;
					long end1 = System.currentTimeMillis();
					
					totalCount ++;
				}
			}
			long end = System.currentTimeMillis();
			out.println("Total Mobile Detect duration:" + (end - start));
		} else {
			out.println("Invalid test data in file /WEB-INF/mobile_test_data.json");
		}
		out.println("Expecting all detections to be mobile : Got " + mobileCount + " Mobiles and "+ normalCount + " Others<br/>");
		out.close();
	}
}
